package com.study.demo.ui.main;

import android.text.TextUtils;

import com.stduy.injector.PerActivity;
import com.study.demo.api.forum.ForumApi;
import com.study.demo.api.game.GameApi;
import com.study.demo.bean.MessageData;
import com.study.demo.bean.Pm;
import com.study.demo.bean.PmData;
import com.study.demo.components.okhttp.OkHttpHelper;
import com.study.demo.util.UpdateAgent;

import dagger.Module;
import dagger.Provides;
import io.reactivex.Observable;

/**
 * Created by shangsong on 17-3-13.
 */

@Module
public class MainModule {
    private final MainActivity mainActivity;

    public MainModule(MainActivity mainActivity) {
        this.mainActivity = mainActivity;
    }

    @Provides
    @PerActivity
    Observable<Integer> provideNotificationObservable(GameApi mGameApi, ForumApi mForumApi) {
        return Observable.zip(mGameApi.queryPmList(""), mForumApi.getMessageList("", 1),
                this::getNotificationSize);
    }

    private Integer getNotificationSize(PmData pmData, MessageData messageData) {
        int size = 0;
        if (pmData != null) {
            if (pmData.is_login == 0) {
                return null;
            }
            for (Pm pm : pmData.result.data) {
                if (!TextUtils.isEmpty(pm.unread) && pm.unread.equals("1")) {
                    size++;
                }
            }
        }
        if (messageData != null && messageData.status == 200) {
            size += messageData.result.list.size();
        }
        return size;
    }


    @Provides
    @PerActivity
    UpdateAgent provideUpdateAgent(OkHttpHelper mOkHttpHelper) {
        return new UpdateAgent(mOkHttpHelper, mainActivity);
    }
}
